package com.lucker.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lucker.common.util.HttpUtil;
import com.lucker.common.util.MD5;

public class PushOrderExpress {
	
	static String code = "jckjProduct";
	static String key = "fae05ee808e562f663fa6ce711d70dab";
	static String baseUrl = "http://jckjclub.pusai.net/thirdParty/";
	
	
	
	public static Map<String,String> emap = new HashMap<String,String>();
	public static void init() {
		
		emap.put("顺丰速运","1");
		emap.put("圆通速递","2");
		emap.put("申通快递","3");
		emap.put("韵达速递","4");
		emap.put("百世快递(原汇通)","5");
		emap.put("EMS","6");
		emap.put("中国邮政快递/包裹/挂号信/小包","7");
		emap.put("天天快递","8");
		emap.put("中通快递","9");
		emap.put("德邦物流","10");
		emap.put("京东快递","28");
		emap.put("品骏快递","29");
		emap.put("安能物流","30");
		emap.put("速尔物流","31");
		emap.put("全峰快递","32");
		emap.put("UC优速快递","33");
		
		
	}
	public static void main(String[] args) {
		try {
			init();
			//getExpress();
			//pushOrder();
			testXlsx();
			
			
			
			
			
			//13
			//pushOrder(""+13,"2","802856832524472649");
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void pushOrder() throws Exception {
		long ts = new Date().getTime();
		Map<String,Object> body = new HashMap<String,Object>();
		body.put("code", code);
		body.put("encryptedString", MD5.passmd5("code="+code+"&ts="+String.valueOf(ts)+"&key="+key));
		body.put("ts",ts);
		
		
		body.put("channelOrderId", "31909");
		body.put("courierId", "9");
		body.put("courierNumber", "75110597197008");
		
		String ret = HttpUtil.post(baseUrl + "order/delivery", body);
		System.out.println(ret);
		
		
	}
	
	
	public static void pushOrder(String orderid,String expid,String expNo) throws Exception {
		long ts = new Date().getTime();
		Map<String,Object> body = new HashMap<String,Object>();
		body.put("code", code);
		body.put("encryptedString", MD5.passmd5("code="+code+"&ts="+String.valueOf(ts)+"&key="+key));
		body.put("ts",ts);
		
		
		body.put("channelOrderId", orderid);
		body.put("courierId", expid);
		body.put("courierNumber", expNo);
		
		String ret = HttpUtil.post(baseUrl + "order/delivery", body);
		System.out.println(ret);
		
		
	}
	
	
	
	public static void getExpress()throws Exception {
		long ts = new Date().getTime();
		Map<String,Object> body = new HashMap<String,Object>();
		body.put("code", code);
		body.put("encryptedString", MD5.passmd5("code="+code+"&ts="+String.valueOf(ts)+"&key="+key));
		body.put("ts",ts);
		
		String url = baseUrl + "order/logisticsCompany";
		System.out.println(url);
		//String ret = HttpUtil.postJson( url, JSONObject.toJSONString(body), null);
		String ret = HttpUtil.get( url, body);
		System.out.println(ret);
		
		
		if(StringUtils.isNotBlank(ret)) {
			JSONObject expobj = JSONObject.parseObject(ret);
			JSONArray arr = expobj.getJSONArray("list");
			for(int i = 0 ; arr != null && i < arr.size() ; i++) {
				JSONObject exp = arr.getJSONObject(i);
				String name = exp.getString("name");
				String id = exp.getString("id");
				System.out.println("emap.put(\""+name+"\",\""+id+"\");");
			}
		}
	}
	
	
	
	public static void testXlsx() throws Exception {
	    File file = new File("H:\\11\\chenchen111.xlsx");
	    System.out.println(file.exists());
	    //获取输入流  
	    InputStream stream = new FileInputStream(file); 
	    Workbook xssfWorkbook = new XSSFWorkbook(stream); 
	    Sheet sheet = xssfWorkbook.getSheetAt(3); 
	    
	    //获得当前sheet的开始行
        int firstRowNum  = sheet.getFirstRowNum();
        //获得当前sheet的结束行
        int lastRowNum = sheet.getLastRowNum();
        //循环除了第一行的所有行
        int count = 0;
        for(int rowNum = firstRowNum+0;rowNum <= lastRowNum;rowNum++){ //为了过滤到第一行因为我的第一行是数据库的列
        	//获得当前行
        	Row row = sheet.getRow(rowNum);
        	//Cell cell1 = row.getCell(0);
        	Cell cell2 = row.getCell(1);
        	Cell cell3 = row.getCell(2);
        	Cell cell4 = row.getCell(3);
        	//System.out.println("cell2" + cell2.getStringCellValue());
        	String orderid = null;
        	if(cell2 != null) {
        		//orderid = String.valueOf(new Double(cell2.getNumericCellValue()).intValue());
        		orderid = cell2.getStringCellValue();
        	}
        	
        	//System.out.println("cell3");
        	String expname = null;
        	if(cell3 != null) {
        		expname = cell3.getStringCellValue();
        	}
        	
        	//System.out.println("cell4");
        	String expNo = null;
        	if(cell4 != null) {
        		expNo = String.valueOf(cell4.getStringCellValue());
        	}
        	      	
        	

        	if(StringUtils.isNotBlank(orderid) && StringUtils.isNotBlank(expname) && StringUtils.isNotBlank(expNo)) {
        		count++;
        		String expid = emap.get(expname);
        		System.out.println(count + " :   " + orderid + "----------------" + expname + "----------- " + expNo + "-----  "  + expid);
//        		if( count > 30 ) {
//        			return;
//        		}
        		pushOrder(orderid,expid,expNo);
        	}
        
        
        }
	}
	
	
	

}
